from . import models
from . import serializers
from django.db.models import Sum
from utils.get_query_params import GetQueryParams
from utils.commonviews import CommonAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from ProjectTool.AutomatedTool import automated_tool
from utils.base_response import BaseResponse


class AutomatedTaskModelViewSet(CommonAPIView):
    query_set = models.AutomatedTask.objects.all().order_by('id')
    serializer_class = serializers.AutomatedTaskSerializers
    input_get_list = [('automated_task_id', 'id'),
                      ('automated_task_project', 'project'),
                      ('automated_task_module', 'module'),
                      ('automated_task_test_person', 'test_person'),
                      ('automated_task_status', 'status'),
                      ('automated_task_property', 'property'),
                      ('automated_task_describe', 'describe'),
                      ('automated_task_title', 'title__contains'),
                      ('inputObjName', 'title__contains')]
    input_put_list = [('automated_task_id', 'id')]
    input_delete_list = [('automated_task_id', 'id')]
    input_dict = GetQueryParams


class AutomatedTaskRecordModelViewSet(CommonAPIView):
    query_set = models.AutomatedTaskRecord.objects.all().order_by('-id')
    serializer_class = serializers.AutomatedTaskRecordSerializers
    input_get_list = [
        ('automated_task_record_id', 'id'),
        ('automated_task_record_task', 'task'),
        ('automated_task_record_automated_task', 'automated_task'),
        ('automated_task_record_status', 'status'),
    ]
    input_put_list = [('automated_task_record_id', 'id')]
    input_delete_list = [('automated_task_record_id', 'id')]
    input_dict = GetQueryParams


# 测试报告视图
class TestReport(APIView):
    def post(self, request):
        res = BaseResponse()
        data = request.data
        report_status = data.pop('report_status') if data.get('report_status') else 0
        query_set = models.AutomatedTaskRecord.objects.filter(**data)
        if query_set.count():
            select_data = {
                'cases_sum': Sum('cases_sum'),
                'cases_execute_sum': Sum('cases_execute_sum'),
                'cases_failure_sum': Sum('cases_failure_sum'),
                'cases_not_execute_sum': Sum('cases_not_execute_sum'),
                'cases_detail_sum': Sum('cases_detail_sum'),
                'cases_detail_execute_sum': Sum('cases_detail_execute_sum'),
                'cases_detail_failure_sum': Sum('cases_detail_failure_sum'),
                'cases_detail_not_execute_sum': Sum('cases_detail_not_execute_sum'),
            }
            if report_status == 'report_day':
                res.data = query_set.aggregate(**select_data)
            elif report_status == 'report_weeks':
                report_data = query_set.extra(select={"create_time": "DATE_FORMAT(create_time,'%%Y-%%m-%%d')"})\
                    .values('create_time').annotate(**select_data)
                res.data = {item['create_time']: item for item in report_data}
            elif report_status == 'report_years':
                report_data = query_set.extra(select={"create_time": "DATE_FORMAT(create_time,'%%Y-%%m')"}) \
                    .values('create_time').annotate(**select_data)
                res.data = {item['create_time']: item for item in report_data}

        else:
            res.status = 1
            res.msg = '未找到数据！'
        return Response(res.dict)


class TestAutomated(APIView):
    def post(self, request):
        automated_id = request.data.get('id')
        res = automated_tool.test_cases(1, id=automated_id)
        return Response(res.dict)
